/**
 * GeoSOA/olap4js/CellSetAxis.js
 *
 * CellSetAxis object
 *
 * Copyright (C) 2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090421
 **/

dojo.provide("GeoSOA.olap4js.CellSetAxis");

dojo.require("GeoSOA.olap4js.CellSet");
dojo.require("GeoSOA.olap4js.AxisMeta");


/**
 * CellSetAxis object
 * 
 * @param {Object} jsonCellSetAxis
 * @param {Number} ordinal The ordinal of this CellSetAxis (-1 if filter axis)
 * @param {Object} cellSet CellSet object this CellSetMetaData belongs to
 *
 * @constructor
  */
GeoSOA.olap4js.CellSetAxis = function (jsonCellSetAxis, ordinal, cellSet) {
	
	var i, j, jsonMemberSet, memberSet;
	
	this.cellSet = cellSet;
	
	// -1 axis ordinal is for the filter (slicer) axis
	this.ordinal = ordinal;
	
	this.members = [];
	for (i = 0; i < jsonCellSetAxis.members.length; i++) {
		jsonMemberSet = jsonCellSetAxis.members[i];
		memberSet = [];
		this.members[i] = memberSet;
		for (j = 0; j < jsonMemberSet.length; j++) {
			memberSet[j] = new GeoSOA.olap4js.Member(jsonMemberSet[j], this.getAxisMeta());
		}
	}
	
	this.positions = [];
	for (i = 0; i < jsonCellSetAxis.positions.length; i++) {
		this.positions[i] = new GeoSOA.olap4js.Position(jsonCellSetAxis.positions[i], i, this); 
	}
	
};

// CellSetAxis methods

/**
 * Returns the AxisMeta object corresponding to this CellSetAxis
 * 
 * @return The AxisMeta object
 * @type Object
 * @public
 */
GeoSOA.olap4js.CellSetAxis.prototype.getAxisMeta = function () {
	return this.cellSet.metadata.getAxisMeta(this.ordinal);
};


/**
 * Position object
 * 
 * @param {Object} jsonPosition
 * @param {Number} ordinal The ordinal of this Position
 * @param {Object} cellSet CellSetAxis object this Position belongs to
 *
 * @constructor
 */
GeoSOA.olap4js.Position = function (jsonPosition, ordinal, cellSetAxis) {

	var i, memberIdx;
	
	this.cellSetAxis = cellSetAxis;
	this.ordinal = ordinal;
		
	// we need to pass along the CellSetMetaData object so the members
	// can identify which level & hierarchy they belong to
	var axisMeta = cellSetAxis.cellSet.metadata.getAxisMeta(cellSetAxis.ordinal);
	
	this.members = [];
	for (i = 0; i < jsonPosition.length; i++) {
		// retrieve ref to member in the axis' member set
		memberIdx = jsonPosition[i];
		this.members[i] = this.cellSetAxis.members[i][memberIdx]; 
	}
};


/**
 * Member object
 * 
 * @param {Object} jsonMember
 * @param {Object} axisMeta AxisMeta object used to lookup the Level and
 *        Hierarchy of this Member
 *
 * @constructor
 */
GeoSOA.olap4js.Member = function (jsonMember, axisMeta) {
	
	// this.position = position;
	
	this.name = jsonMember.name;
	
	//this.hierarchy = jsonMember.hierarchy; // TODO: ref to metadata hierarchy instead
	this.hierarchy = axisMeta.getHierarchyByName(jsonMember.hierarchy);
	
	this.dimension = this.hierarchy.dimensionName;
	
	this.level = this.hierarchy.getLevelByName(jsonMember.level);
	
	// can't do ref to parent member (it may not have been retrieved in this cellset),
	// so keep its name instead
	this.parent = jsonMember.parent;
	
	// this is the object containing property values
	this.properties = jsonMember.properties;
	
};
